package com.lakeworks.mapper;

import com.lakeworks.common.domain.channel.AttachmentInfo;
import com.lakeworks.domain.OprAttachment;
import com.lakeworks.domain.vo.OprAttachmentVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 入厂附件Mapper接口
 *
 * @author lake
 * @date 2023-02-08
 */
public interface OprAttachmentMapper {
    /**
     * 查询入厂附件
     *
     * @param id 入厂附件主键
     * @return 入厂附件
     */
    OprAttachment selectOprAttachmentById(Long id);

    /**
     * 查询入厂附件列表
     *
     * @param oprAttachment 入厂附件
     * @return 入厂附件集合
     */
    List<OprAttachment> selectOprAttachmentList(OprAttachment oprAttachment);

    /**
     * 新增入厂附件
     *
     * @param oprAttachment 入厂附件
     * @return 结果
     */
    int insertOprAttachment(OprAttachment oprAttachment);

    /**
     * 修改入厂附件
     *
     * @param oprAttachment 入厂附件
     * @return 结果
     */
    int updateOprAttachment(OprAttachment oprAttachment);

    /**
     * 删除入厂附件
     *
     * @param id 入厂附件主键
     * @return 结果
     */
    int deleteOprAttachmentById(Long id);

    /**
     * 批量删除入厂附件
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    int deleteOprAttachmentByIds(String[] ids);

    OprAttachment queryByBarcode(@Param("barcode") String barcode);

    OprAttachment queryByBarcodeAndProductId(@Param("productId") Long productId, @Param("barcode") String barcode);

    List<OprAttachment> queryByProductId(@Param("productId") Long productId);

    List<AttachmentInfo> selectByProductId(Long productId);

    List<OprAttachment> queryByProductIdOrCompanionId(@Param("productId") Long productId);

    void deleteByProductId(@Param("productId") Long productId);

    List<OprAttachment> selectAttachmentList(Long productId);

    @Select("select oat.id,oat.product_id productId,oat.barcode,oat.frame_no frameNo,\n" +
            "sat.name attName,oat.attach_time attachTime,oat.flag flag,\n" +
            "IF(oat.is_scan = 1,'是','否') isScan\n" +
            "from opr_attachment oat\n" +
            "left join sys_attachment sat\n" +
            "on oat.attachment_id = sat.id\n" +
            "where oat.product_id = #{productId}\n" +
            "and oat.flag & 512 != 512")
    List<OprAttachmentVO> listOprAttVoByProId(@Param("productId") Long productId);

    @Select("SELECT COUNT(id) FROM opr_attachment\n" +
            "WHERE product_id = #{productId}\n" +
            "AND flag & 512 != 512")
    Integer countOprAttVoByProId(@Param("productId") Long productId);

    List<OprAttachment> listByPickupId(@Param("pickupId") Long pickupId);
}
